package com.hww.doc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hww.doc.business.annotation.RedisCache;
import com.hww.doc.business.entity.Books;
import com.hww.doc.business.entity.Itemsets;
import com.hww.doc.business.service.DocBookService;
import com.hww.doc.business.service.DocItemsetService;
import com.hww.doc.business.vo.BooksConditionVO;
import com.hww.doc.business.vo.ItemsetsConditionVO;
import com.hww.doc.persistence.beans.*;
import com.hww.doc.persistence.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 空间列表
 *
 * @author yaxu.huang
 * @version 1.0
 * @website http://www.fczd.online
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Slf4j
@Service
public class DocBookServiceImpl implements DocBookService {

	@Autowired
	private DocBooksMapper doBooksMapper;

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Books> findPageBreakByCondition(BooksConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<DocBooks> list = doBooksMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<Books> boList = new LinkedList<>();
        for (DocBooks docBooks : list) {
            boList.add(new Books(docBooks));
        }
        PageInfo bean = new PageInfo<DocBooks>(list);
        bean.setList(boList);
        return bean;
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	@RedisCache(flush = true)
	public Books insert(Books entity) {
		Assert.notNull(entity, "Itemset不可为空");
		entity.setCreateTime(new Date());
		doBooksMapper.insertSelective(entity.getDocBooks());
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@RedisCache(flush = true)
	public boolean removeByPrimaryKey(Long primaryKey) {
		boolean result = doBooksMapper.deleteByPrimaryKey(primaryKey) > 0;
		//空间删除意味着项目也需要删除 需增加关联控制
		return false;
	}

	@Override
	@RedisCache(flush = true)
	public boolean updateSelective(Books entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Books getByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}
}
